package com.crane.common.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.SingleResponse;
import com.crane.oss.common.bean.request.QueryDistrictRequest;
import com.crane.oss.sys.bean.request.dict.QueryDictDetailReq;
import com.crane.oss.sys.bean.resp.DictDetailResp;
import com.crane.oss.common.service.DistrictService;
import com.crane.oss.sys.service.DictDetailService;
import com.crane.oss.web.BaseBusinessController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author bealon
 * @email ccsu123456@qq.com
 * @date 2019年01月08日 19时38分36秒
 */

@Api(value = "CommonController", tags = {"字典与地区"})
@RestController
@RequestMapping("/api/common")
public class CommonController extends BaseBusinessController {


    @Autowired
    private DistrictService districtService;

    @Autowired
    DictDetailService dictDetailService;

    @ApiOperation(value = "获取字典列表", notes = "获取字典列表")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryDictDetailReq")
    @RequiresAuthentication
    @PostMapping("/queryDictList")
    public MultiResponse<Map<String, Object>> queryDictList(@RequestBody QueryDictDetailReq request) {
        // 查询列表数据
    	request.setPageSize(100);
        IPage<DictDetailResp> page = dictDetailService.queryList(request);

        List<Map<String, Object>> list = new ArrayList<>();
        if (page.getRecords().size() != 0) {
            page.getRecords().forEach(s -> {
                Map<String, Object> map = new HashMap<>();
                map.put("key",  s.getId());
                map.put("name", s.getDictName());
                map.put("value", s.getDictValue());
                list.add(map);
            });
        }
        return MultiResponse.ofWithoutTotal(list);
    }


    @ApiOperation(value = "获取地区列表信息", notes = "获取地区列表信息")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryDistrictRequest")
    @RequiresAuthentication
    @PostMapping("/district/List")
    public MultiResponse<Map<String, Object>> queryDistrictList(@RequestBody QueryDistrictRequest request) {
        // 查询列表数据
        List<Map<String, Object>> list = districtService.queryList(request);
        return MultiResponse.ofWithoutTotal(list);
    }

    @SuppressWarnings("rawtypes")
	@ApiOperation(value = "获取地区名称", notes = "获取地区名称")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryDistrictRequest")
    @RequiresAuthentication
    @PostMapping("/district/name")
    public SingleResponse<String> queryName(@RequestBody QueryDistrictRequest request) {
        // 查询列表数据
        String name = districtService.queryName(request);
        return SingleResponse.of(name);
    }


}
